package com.zhl.hadoop.cases.joincase;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;
/**
products.txt:
 id	name	catagary_id	amount
 1	小米		c001		2000
 2	apple	c002		5000
 3	apple	c001		8000

orders.txt:
 id	catagary_id	order_date
 1	c001		20201030
 2	c002		20201029
*/
/**
 * @program: demos
 * @description: 将商品表和订单表关联，查询订单的商品信息
 * @author: 刘振华
 * @create: 2020-10-30 17:36
 **/
public class JoinMapper extends Mapper<LongWritable, Text,Text,Text> {
	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		String[] strs = value.toString().split("\t");
		//1.判断来自哪个文件
		FileSplit fs = (FileSplit)context.getInputSplit();
		if(fs.getPath().getName().endsWith("orders.txt")){
			//来自订单表
			context.write(new Text(strs[1]),new Text(strs[2]));
		}else {
			//来自商品表
			context.write(new Text(strs[2]),new Text(strs[1]+"\t"+strs[3]));
		}
	}
}
